﻿Module Module1
    

    Sub Main()
        Dim stream As Object
#If DEBUG Then
        stream = New System.IO.StreamReader("\\STAZIONE_D5\Drawsearch Suite for Lafer\Temp\filedaaggiornare")
#Else
        stream = new System.IO.StreamReader(My.Application.Info.DirectoryPath & "\Temp\filedaaggiornare")
#End If

        Dim listafile As New ArrayList

        Do Until stream.EndOfStream
            Dim RIGA = stream.READLINE
            If RIGA = Nothing Then
            Else
                listafile.Add(RIGA)
                Console.WriteLine(RIGA)
            End If
        Loop
        stream.Close()
        Console.WriteLine("LISTA FILE CARICATA")
        ' Console.ReadKey()

        Dim processi = Process.GetProcessesByName("DrawSearch")
        If processi.Length > 0 Then
            processi(0).CloseMainWindow()
            processi(0).WaitForExit()
            Do Until processi(0).HasExited
                If processi(0).HasExited = True Then
                    Exit Do
                End If
            Loop
        End If
        Console.WriteLine("PROCESSO CHIUSO")
        If listafile.Count > 0 Then
            For Each file In listafile
                If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath.ToString & "\" & file.ToString) Then
                    If My.Computer.FileSystem.FileExists("\\LAFER2K3\Uff_tec\PDF_PROE\Drawsearch_host\" & file.ToString) Then
                        Console.WriteLine("COPIO IL FILE:" & file.ToString)
                        My.Computer.FileSystem.CopyFile("\\LAFER2K3\Uff_tec\PDF_PROE\Drawsearch_host\" & file, My.Application.Info.DirectoryPath.ToString & "\" & file, True)
                        Console.WriteLine("file copiato con successo" & file.ToString)
                    End If
                Else
                    My.Computer.FileSystem.CopyFile("\\LAFER2K3\Uff_tec\PDF_PROE\Drawsearch_host\" & file, My.Application.Info.DirectoryPath.ToString & "\" & file, True)
                    Console.WriteLine("file copiato con successo" & file.ToString)
                End If
            Next
        End If

        'Console.ReadKey()

        Console.WriteLine("FILE COPIATI")
        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath.ToString & "\Temp\filedaaggiornare") Then
            Console.WriteLine("CANCELLO LA LISTA DEGLI AGGIORNAMENTI")
            My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath.ToString & "\Temp\filedaaggiornare")
        ElseIf My.Computer.FileSystem.FileExists(My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\DrawSearch Suite for Lafer\Temp\filedaaggiornare") Then
            My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\DrawSearch Suite for Lafer\Temp\filedaaggiornare")
        End If
        Console.WriteLine("FILE AGGIORNAMENTO ELIMINATO")

        ' Console.ReadKey()

        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath.ToString & "\DrawSearch.exe") Then
            Console.WriteLine("apro c:\ drawsearc")
            Process.Start(My.Application.Info.DirectoryPath.ToString & "\DrawSearch.exe")
        End If

    End Sub

End Module
